<?php
session_start();
if(empty($_SESSION['user'])){
    echo "请先登录";
    $url='/index.php';
    echo "<a href='".$url."'>".'返回首页'."</a>";
    die();
}
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<?php
include "functions.php";
$bookid=$_POST['bookid'];
$bookname=$_POST['bookname'];

if(!effectBook($bookid)){
    echo "书编号不存在或者已经借出<br>";
    $url1='/borrow.php';
    echo "<a href='".$url1."'>".'重新进行借书操作'."</a><br>";
    $url2='/index.php';
    echo "<a href='".$url2."'>".'返回首页'."</a>";
    die();
}


//判断该用户是否已经借了3本书，或者有一本逾期未还
$info=UserBorrow($_SESSION['user']);
if(count($info)>=3){
    echo "您目前已经借了3本书，归还前不能执行借书操作";
    $url='/index.php';
    echo "<a href='".$url."'>".'返回首页'."</a>";
    die();
}else {
    foreach ($info as $value){
        $borrow=(time()-strtotime(trim($value['BorrowTime'])));
        if($borrow>(3*60*60*24)){
            echo "您有书逾期未还，不能继续借书";
            $url='/index.php';
            echo "<a href='".$url."'>".'返回首页'."</a>";
            die();
        }
    }
}

addRecord($_SESSION['user'],$bookid);

//把数据库中书表的该书状态改为借出：0
$flag='0';
updateBookStatus($bookid,$flag);

echo "借阅成功！请于3日内归还\n";

$url='/index.php';
echo "<a href='".$url."'>".'返回首页'."</a>";
?>



</body>
</html>
